#! /bin/sh

if which ncursesw6-config >/dev/null
then
	echo 'using ncursesw6 autoconfiguration script…'
	NCURSES_FLAGS=$(ncursesw6-config --cflags)
	NCURSES_LIBS=$(ncursesw6-config --libs)

elif which ncursesw5-config >/dev/null
then
	echo 'using ncursesw5 autoconfiguration script…'
	NCURSES_FLAGS=$(ncursesw5-config --cflags)
	NCURSES_LIBS=$(ncursesw5-config --libs)
elif which ncurses6-config >/dev/null
then
	echo 'using ncurses6 autoconfiguration script…'
	NCURSES_FLAGS=$(ncurses6-config --cflags)
	NCURSES_LIBS=$(ncurses6-config --libs)

elif which ncurses5-config >/dev/null
then
	echo 'using ncurses5 autoconfiguration script…'
	NCURSES_FLAGS=$(ncurses5-config --cflags)
	NCURSES_LIBS=$(ncurses5-config --libs)

elif which pkg-config >/dev/null
then
	echo 'using  pkg-config…'
	NCURSES_FLAGS=$(pkg-config ncursesw --cflags)
	NCURSES_LIBS=$(pkg-config ncursesw --libs)
else
	echo 'can not configure! you will have to manually specify flags.'
	NCURSES_FLAGS='# manual configuration required'
	NCURSES_LIBS='# manual configuration required'
fi

if [ $(uname -s) == 'FreeBSD' ]
then
	NCURSES_FLAGS="-I/usr/local/include $NCURSES_FLAGS"
fi

echo "# automatically generated build configuration

NCURSES_FLAGS  = $NCURSES_FLAGS
NCURSES_LIBS   = $NCURSES_LIBS

CFLAGS   += -std=c89 -pedantic -Wall -Wextra -Werror
CPPFLAGS += -Iincs \$(NCURSES_FLAGS)
LDFLAGS  += \$(NCURSES_LIBS)

CC ?= cc" > config.mk
echo 'wrote config.mk.'

